
import { timeFn } from '@/utils/index'
export default {
  data() {
    return {
      tagType: {
        selfService: {
          '1': 'warning', // 编辑
          '2': 'info', // 待派发
          '50': 'success', // 已完成
          '90': 'success', // 升级处理完成
          '11': 'danger', // "升级处理中"
          '20': 'danger', // "处理中"
          '5': '', // 未确认
          '10': '', // 未接收
          '70': 'info' // 升级中
        }
      },
      ruleForm: {
        workOrderNo: null,
        systemName: null,
        eventStatus: null,
        eventLevel: null,
        eventSource: null,
        faultType: null,
        area: null,
        declarer: null,
        receiver: null,
        faultPhenomenon: null
      },
      dialogVisible: false,
      searchOptions: {
        'parent': [
          {
            value: 'archiveFlag',
            key: 'archiveFlag',
            label: '归档标记',
            type: 'input',
            placeholder: '请输入归档标记'
          },
          {
            value: 'psrNum',
            key: 'psrNum',
            label: '产品规范码',
            type: 'input',
            placeholder: '请输入产品规范码'
          },
          {
            value: 'lastDate',
            key: 'lastDate',
            label: '维护日期',
            type: 'date',
            valueFormat: 'yyyyMMdd',
            placeholder: '请选择维护日期'
          },
          {
            value: 'userId',
            key: 'userId',
            label: '维护者',
            type: 'input',
            placeholder: '请输入维护者'
          },
          {
            value: 'edgeType',
            key: 'edgeType',
            label: '切边形态',
            type: 'input',
            placeholder: '请输入切边形态'
          },
          {
            value: 'holdingDiv',
            key: 'holdingDiv',
            label: '封锁区分',
            type: 'input',
            placeholder: '请输入封锁区分'
          },
          {
            value: 'guideMeasure',
            key: 'guideMeasure',
            label: '镀层计量方式',
            type: 'input',
            placeholder: '请输入镀层计量方式'
          },
          {
            value: 'qcType',
            key: 'qcType',
            label: '质保书类型',
            type: 'input',
            placeholder: '请输入质保书类型'
          },
          {
            value: 'qcNum',
            key: 'qcNum',
            label: '质保书份数',
            type: 'input',
            placeholder: '请输入质保书份数'
          },
          {
            value: 'weightMode',
            key: 'weightMode',
            label: '计重方式',
            type: 'input',
            placeholder: '请输入计重方式'
          },
          {
            value: 'checkDivision',
            key: 'checkDivision',
            label: '验收部门',
            type: 'input',
            placeholder: '请输入验收部门'
          },
          {
            value: 'railCode',
            key: 'railCode',
            label: '轨梁产品截面代码',
            type: 'input',
            placeholder: '请输入轨梁产品截面代码'
          },
          {
            value: 'railCodeDesc',
            key: 'railCodeDesc',
            label: '轨梁产品截面描述',
            type: 'input',
            placeholder: '请输入轨梁产品截面描述'
          },
          {
            value: 'orderTechnicDesc',
            key: 'orderTechnicDesc',
            label: '合同单技术参数描述',
            type: 'input',
            placeholder: '请输入合同单技术参数描述'
          },
          {
            value: 'recogniseCode',
            key: 'recogniseCode',
            label: '产品标识代码',
            type: 'input',
            placeholder: '请输入产品标识代码'
          }
        ],
        'first': [
          {
            value: 'archiveFlag',
            key: 'archiveFlag',
            label: '归档标记',
            type: 'input',
            placeholder: '请输入归档标记'
          },
          {
            value: 'psrNum',
            key: 'psrNum',
            label: '产品规范码',
            type: 'input',
            placeholder: '请输入产品规范码'
          },
          {
            value: 'function',
            key: 'function',
            label: '叙述段落区分',
            type: 'input',
            placeholder: '请输入叙述段落区分'
          },
          {
            value: 'lineSeq',
            key: 'lineSeq',
            label: '叙述行序',
            type: 'input',
            placeholder: '请输入叙述行序'
          },
          {
            value: 'description',
            key: 'description',
            label: '叙述内容',
            type: 'input',
            placeholder: '请输入叙述内容'
          },
          {
            value: 'lastDate',
            key: 'lastDate',
            label: '维护日期',
            type: 'date',
            valueFormat: 'yyyyMMdd',
            placeholder: '请选择维护日期'
          },
          {
            value: 'userId',
            key: 'userId',
            label: '维护者',
            type: 'input',
            placeholder: '请输入维护者'
          }
        ],
        'second': [
          {
            value: 'archiveFlag',
            key: 'archiveFlag',
            label: '归档标记',
            type: 'input',
            placeholder: '请输入归档标记'
          },
          {
            value: 'psrNum',
            key: 'psrNum',
            label: '产品规范码',
            type: 'input',
            placeholder: '请输入产品规范码'
          },
          {
            value: 'apnNum',
            key: 'apnNum',
            label: '用途代码',
            type: 'input',
            placeholder: '请输入用途代码'
          },
          {
            value: 'custNum',
            key: 'custNum',
            label: '用户代码',
            type: 'input',
            placeholder: '请输入用户代码'
          },
          {
            value: 'lastDate',
            key: 'lastDate',
            label: '维护日期',
            type: 'date',
            valueFormat: 'yyyyMMdd',
            placeholder: '请选择维护日期'
          },
          {
            value: 'userId',
            key: 'userId',
            label: '维护者',
            type: 'input',
            placeholder: '请输入维护者'
          },
          {
            value: 'desc',
            key: 'desc',
            label: '说明',
            type: 'input',
            placeholder: '请输入说明'
          },
          {
            value: 'tradeKind1',
            key: 'tradeKind1',
            label: '行业类别1',
            type: 'input',
            placeholder: '请输入行业类别1'
          },
          {
            value: 'tradeKind2',
            key: 'tradeKind2',
            label: '行业类别2',
            type: 'input',
            placeholder: '请输入行业类别2'
          },
          {
            value: 'tradeKind3',
            key: 'tradeKind3',
            label: '行业类别3',
            type: 'input',
            placeholder: '请输入行业类别3'
          }
        ]
      }
    }
  },
  computed: {
    tablecolumn() {
       return [
          {
            width: '55',
            fixed: 'left',
            type: 'selection',
            reserveSelection: true,
            align: 'center'
          },
          {
            width: '50',
            render: (h, params) => {
              const elStyle = this.warningTip(params.row)
              return h('div', [
                h('i', elStyle, '')
              ])
            }
          },
          {
            minWidth: '200',
            label: '工单号',
            prop: 'workOrderNo',
            render: (h, params) => {
              return h('a', {
               style: {
                 color: '#1890ff',
                 cursor: 'pointer'
               },
               on: {
                click: () => {
                  this.openDetailWin(params)
                }
              }
              }, params.row.workOrderNo)
            }
          },
          {
            minWidth: '200',
            label: '故障现象',
            align: 'left',
            render: (h, params) => {
              return h('div', {
                style: {
                  fontSize: '18px',
                  color: '#f56c6c'
                }
              }, params.row.faultPhenomenon)
            }
          },
          {
            minWidth: '200',
            label: '运维系统',
            prop: 'systemName'
          },
          {
            label: '事件状态',
            minWidth: '120',
            showOverflowTooltip: true,
            render: (h, params) => {
              return h('el-tag', {
                props: {
                  effect: 'plain',
                  type: this.tagType['selfService'][params.row.eventStatus]
                }
              }, this.codesList && this.codesList['SJZT'] && this.codesList['SJZT'][params.row.eventStatus])
            }
          },
          {
            label: '派发时间',
            minWidth: '180',
            align: 'center',
            render: (h, params) => {
              return this.countAssignTime(h, params)
            }
          },
          {
            label: '故障申报人',
            minWidth: '200',
            align: 'center',
            render: (h, params) => {
              return h('div', {}, [
                h('p', params.row.declarerUnit),
                h('p', `${params.row.declarer} ${params.row.linkTel}`)
              ])
            }
          },
          {
            minWidth: '200',
            align: 'center',
            label: '接收人',
            render: (h, params) => {
              try {
                const curYear = new Date().getFullYear()
              let receiveTime = params.row.receiveTime
              if (receiveTime && params.row.receiveTime.indexOf(curYear) === 0) {
                receiveTime = receiveTime.split((curYear + '-'))[1]
                return h('div', {}, [
                  h('p', params.row.receiver),
                  h('p', receiveTime)
                ])
              } else {
                return h('div', '--')
              }
              } catch (e) { console.log(e) }
            }
          },
          {
            label: '事件等级',
            minWidth: '120',
            showOverflowTooltip: true,
            render: (h, params) => {
              return h('div', this.codesList && this.codesList.SJDJ && this.codesList.SJDJ[params.row.eventLevel])
            }
          },
           {
            label: '事件来源',
            minWidth: '120',
            showOverflowTooltip: true,
            render: (h, params) => {
              return h('div', this.codesList && this.codesList.SJLY && this.codesList.SJLY[params.row.eventSource])
            }
          },
          {
            prop: 'linkAddress',
            minWidth: '130',
            showOverflowTooltip: true,
            label: '联系地址'
          },
          {
            label: '故障类型',
            minWidth: '120',
            showOverflowTooltip: true,
            render: (h, params) => {
              return h('div', this.codesList && this.codesList.GZLX && this.codesList.GZLX[params.row.faultType])
            }
          },
          {
            label: '区域',
            minWidth: '80',
            showOverflowTooltip: true,
            render: (h, params) => {
              return h('div', this.codesList && this.codesList.QUYU && this.codesList.QUYU[params.row.area])
            }
          },
          {
            width: '120',
            showOverflowTooltip: true,
            align: 'center',
            prop: 'creator',
            label: '创建人工号'
          },
          {
            prop: 'remarks',
            minWidth: '200',
            showOverflowTooltip: true,
            label: '备注'
          }
        ]
    },
    // 产品规范基本表
    basicColumn() {
      return [
        {
          width: '55',
          fixed: 'left',
          type: 'selection',
          reserveSelection: true,
          align: 'center'
        },
        {
          prop: 'archiveFlag',
          label: '归档标记',
          width: '120',
          showOverflowTooltip: true,
          align: 'center'
        },
        // {
        //   prop: 'psrNum',
        //   label: '产品规范编码',
        //   width: '120',
        //   showOverflowTooltip: true,
        //   align: 'center'
        // },
        {
          prop: 'psrNum',
          label: '产品规范码',
          width: '120',
          showOverflowTooltip: true,
          align: 'center'
        },
        {
          prop: 'lastDate',
          label: '维护日期',
          width: '120',
          showOverflowTooltip: true,
          align: 'center',
          render: (h, params) => {
            let r = ''
            if (params.row.lastDate) {
              r = params.row.lastDate.replace(/^(\d{4})(\d{2})(\d{2})$/, '$1-$2-$3')
            }
            return h('div', r)
          }
        },
        {
          prop: 'userId',
          label: '维护者',
          width: '120',
          showOverflowTooltip: true,
          align: 'center'
        },
        {
          prop: 'edgeType',
          label: '切边形态',
          width: '120',
          showOverflowTooltip: true,
          align: 'center'
        },
        {
          prop: 'holdingDiv',
          label: '封锁区分',
          width: '120',
          showOverflowTooltip: true,
          align: 'center'
        },
        {
          prop: 'guideMeasure',
          label: '镀层计量方式',
          width: '120',
          showOverflowTooltip: true,
          align: 'center'
        },
        {
          prop: 'qcType',
          label: '质保书类型',
          width: '120',
          showOverflowTooltip: true,
          align: 'center'
        },
        {
          prop: 'qcNum',
          label: '质保书份数',
          width: '120',
          showOverflowTooltip: true,
          align: 'center'
        },
        {
          prop: 'weightMode',
          label: '计重方式',
          width: '120',
          showOverflowTooltip: true,
          align: 'center'
        },
        {
          prop: 'checkDivision',
          label: '验收部门',
          width: '120',
          showOverflowTooltip: true,
          align: 'center'
        },
        {
          prop: 'railCode',
          label: '轨梁产品截面代码',
          width: '120',
          showOverflowTooltip: true,
          align: 'center'
        },
        {
          prop: 'railCodeDesc',
          label: '轨梁产品截面描述',
          width: '120',
          showOverflowTooltip: true,
          align: 'center'
        },
        {
          prop: 'orderTechnicDesc',
          label: '合同单技术参数描述',
          width: '120',
          showOverflowTooltip: true,
          align: 'center'
        },
        {
          prop: 'recogniseCode',
          label: '产品标识代码',
          width: '120',
          showOverflowTooltip: true,
          align: 'center'
        }
      ]
    },
    basicChildColumn() {
      if (this.activeName === 'first') { // 产品规范标准叙述表
        return [
          {
            width: '55',
            fixed: 'left',
            type: 'selection',
            reserveSelection: true,
            align: 'center'
          },
          {
            prop: 'archiveFlag',
            label: '归档标记',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            prop: 'psrNum',
            label: '产品规范码',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            prop: 'function',
            label: '叙述段落区分',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            prop: 'lineSeq',
            label: '叙述行序',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            prop: 'description',
            label: '叙述内容',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            prop: 'lastDate',
            label: '维护日期',
            showOverflowTooltip: true,
            align: 'center',
            render: (h, params) => {
              let r = ''
              if (params.row.lastDate) {
                r = params.row.lastDate.replace(/^(\d{4})(\d{2})(\d{2})$/, '$1-$2-$3')
              }
              return h('div', r)
            }
          },
          {
            prop: 'userId',
            label: '维护者',
            showOverflowTooltip: true,
            align: 'center'
          }
        ]
      } else { // 产品规范用途叙述表
        return [
          {
            width: '55',
            fixed: 'left',
            type: 'selection',
            reserveSelection: true,
            align: 'center'
          },
          {
            prop: 'archiveFlag',
            label: '归档标记',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            prop: 'psrNum',
            label: '产品规范码',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            prop: 'apnNum',
            label: '用途代码',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            prop: 'custNum',
            label: '用户代码',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            prop: 'lastDate',
            label: '维护日期',
            showOverflowTooltip: true,
            align: 'center',
            render: (h, params) => {
              let r = ''
              if (params.row.lastDate) {
                r = params.row.lastDate.replace(/^(\d{4})(\d{2})(\d{2})$/, '$1-$2-$3')
              }
              return h('div', r)
            }
          },
          {
            prop: 'userId',
            label: '维护者',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            prop: 'desc',
            label: '说明',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            prop: 'tradeKind1',
            label: '行业类别1',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            prop: 'tradeKind2',
            label: '行业类别2',
            showOverflowTooltip: true,
            align: 'center'
          },
          {
            prop: 'tradeKind3',
            label: '行业类别3',
            showOverflowTooltip: true,
            align: 'center'
          }
        ]
      }
    }
  },
  methods: {
    openQurey() {
      this.dialogVisible = true
      this.dragDialog && this.dragDialog.rest()
    },
    // 获取值集对应lable
    getCodeLabel(key, code) {
      if (!code) {
        return code || ''
      }
      if (this.codesList[key]) {
        return this.codesList[key][code] || ''
      }
    },
    // 计算派发时间
    countAssignTime(h, params) {
      const rs = [h('p', params.row.declareTime)]
      if (!['15', '4', '17', '19'].includes(params.row.eventStatus)) {
        let preword = '已用时'
        let endtime = ''
        if (params.row.completeTime || params.row.completeTime2) {
          endtime = params.row.completeTime || params.row.completeTime2
          preword = '总用时'
        }
        let time = ''
        if (endtime && params.row.declareTime > endtime) {
          time = timeFn(params.row.declareTime, '')
        } else {
        time = timeFn(params.row.declareTime, endtime)
        }
        rs.push(h('p', preword + `${time}`))
      }
      return h('div', {
        style: {
          color: '#e6a700'
        }
       }, rs)
    },
    // 警告提示
    warningTip(row) {
      if (['13', '14', '15', '4', '17', '18', '19'].includes(row.eventStatus)) {
        return
      }
      if (!(row.warningTime && row.declareTime)) {
        return
      }
      try {
          const errorMs = row.warningTime * 60 * 1000
          const warningMs = (row.warningTime - 10) * 60 * 1000 // 还有10分钟 警告
          const declareDate = row.declareTime
          const now = Date.now()
          if (/-/.test(declareDate)) {
            declareDate.replace(/-/g, '/')
          }
          const declareDateMs = new Date(declareDate)
          const diff = now - declareDateMs
          if (diff >= errorMs) {
            // 超时
            return {
              class: {
                'el-icon-warning': true
              },
              attrs: {
                title: '工单已经超时'
              },
              style: {
                padding: '5px',
                color: 'red'
              }
            }
          } else if ((diff < errorMs) && (diff >= warningMs)) {
            // 警告
            return {
              class: {
                'el-icon-warning': true
              },
              attrs: {
                title: '工单即将超时'
              },
              style: {
                padding: '5px',
                color: '#ffba00'
              }
            }
          }
          return {}
        } catch (e) {
        console.log(e)
        return {}
      }
    }
  }
}
